package com.zhp.service.impl;

import com.zhp.dao.ConJonDao;
import com.zhp.dao.DeptDao;
import com.zhp.dao.MeetDao;
import com.zhp.model.Conference;
import com.zhp.model.Dept;
import com.zhp.service.MeetService;
import com.zhp.utils.IdUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Consumer;

public class MeetServiceImpl implements MeetService {

    private MeetDao meetDao = new MeetDao();
    private DeptDao deptDao = new DeptDao();

    @Override
    public List<Conference> listAll(String deptName, int currentPage, String name) {
        return meetDao.findConferences(deptName, currentPage, name);
    }

    @Override
    public long getConferenceCount(String name) {
        return meetDao.getConferenceTotal(name);
    }

    @Override
    public Object meetInfo(String mId) {
        return meetDao.findInfo(mId);
    }

    @Override
    public Conference findMeetById(String mId) {
        return meetDao.findById(mId);
    }

    @Override
    public boolean endMeet(String mId) {


        return meetDao.endMeet(mId);
    }

    @Override
    public Conference addMeet(String title, String depName, String stime, String userId, String content) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("name", depName);
            List<Dept> depts = deptDao.findByT(map);
            Integer deptId = depts.get(0).getId();
            Calendar date = Calendar.getInstance();
            int year = date.get(Calendar.YEAR);  //年
            int month = date.get(Calendar.MONTH) + 1; //月
            int day = date.get(Calendar.DAY_OF_MONTH); //日
            String toTime = year + "-" + month + "-" + day + " " + stime + ":00";  //完整的时间戳
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date setTime = sdf.parse(toTime);   //字符串时间戳转为 data
            Conference conference = new Conference();
            String cid = IdUtil.nextId();
            conference.setId(cid);
            conference.setDeptName(depName);
            conference.setDeptId(deptId);
            conference.setTitle(title);
            conference.setPublishDate(new Date());
            conference.setStartTime(setTime);
            conference.setStatus(0);
            conference.setUId(Integer.valueOf(userId));
            conference.setContent(content);
            int save = meetDao.save(conference);
            if (save > 0) {
                return conference;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


}
